package com.industrial.im.service.impl;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.domain.DispatchKeyItemManagement;
import com.industrial.im.domain.DispatchKeyItemManagementSchedule;
import com.industrial.im.domain.DispatchTaskExecutionManage;
import com.industrial.im.mapper.DispatchKeyItemManagementMapper;
import com.industrial.im.mapper.DispatchKeyItemManagementSchedulerMapper;
import com.industrial.im.mapper.DispatchTaskExecutionManageMapper;
import com.industrial.im.service.IDispatchKeyItemManagementSchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DispatchKeyItemManagementSchedulerServiceImpl implements IDispatchKeyItemManagementSchedulerService {

    @Autowired
    private DispatchKeyItemManagementSchedulerMapper dispatchKeyItemManagementSchedulerMapper;

    @Autowired
    private DispatchKeyItemManagementMapper dispatchKeyItemManagementMapper;

    @Autowired
    private DispatchTaskExecutionManageMapper dispatchTaskExecutionManageMapper;

    /**
     * <p>
     *      保存工作进度信息
     * </p>
     *
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addScheduler(DispatchKeyItemManagementSchedule params) {

        if(StringUtils.isBlank(params.getPkId())){
            throw new RuntimeException("请先签收后填写工作进度");
        }

        BigDecimal scheduleBd = new BigDecimal(params.getSchedule());
        if(scheduleBd.compareTo(BigDecimal.ZERO) <= 0){
            throw new RuntimeException("填写的进度不可等于0或小于0");
        }

        params.setBaseId(IdUtil.simpleUUID());
        params.setBaseCreateTime(DateUtils.getNowDate());
        params.setBaseCreateBy(SecurityUtils.getUserId());

        params.setBaseUpdateTime(DateUtils.getNowDate());
        params.setBaseUpdateBy(SecurityUtils.getUserId());

        List<DispatchKeyItemManagementSchedule> oldTaskSchedule = dispatchKeyItemManagementSchedulerMapper.selectDispatchKeyItemManagementSchedulerListByPkId(params.getPkId());

        BigDecimal newSchedule = new BigDecimal(params.getSchedule());
        if(newSchedule.compareTo(BigDecimal.valueOf(100L)) > 0){
            throw new RuntimeException("工作进度填写有误，数值不可大于100");
        }

        if(oldTaskSchedule != null && !oldTaskSchedule.isEmpty()){
            String schedule = oldTaskSchedule.get(0).getSchedule();
            BigDecimal oldDbSchedule = new BigDecimal(schedule);
            if(newSchedule.compareTo(oldDbSchedule) <= 0){
                throw new RuntimeException("工作进度的填写不可小于上一次的工作进度");
            }
        }
        dispatchKeyItemManagementSchedulerMapper.addScheduler(params);

        oldTaskSchedule = dispatchKeyItemManagementSchedulerMapper.selectDispatchKeyItemManagementSchedulerListByPkId(params.getPkId());

        if(oldTaskSchedule != null && !oldTaskSchedule.isEmpty()){
            //如果工作进度等于100，修改状态为待验收
            Integer sum = oldTaskSchedule.stream().map(k -> StringUtils.isBlank(k.getSchedule()) ? 0 : Integer.valueOf(k.getSchedule())).reduce(0, Integer::sum);
            if(sum >= 100){
                DispatchKeyItemManagement updateParams = new DispatchKeyItemManagement();
                DispatchTaskExecutionManage dispatchTaskExecutionManage = dispatchTaskExecutionManageMapper.selectDispatchTaskExecutionManageByBaseId(params.getPkId());
                if(dispatchTaskExecutionManage != null && StringUtils.isNotBlank(dispatchTaskExecutionManage.getPkId())){
                    updateParams.setBaseId(dispatchTaskExecutionManage.getPkId());
                    updateParams.setItemStatus("2");
                    dispatchKeyItemManagementMapper.updateDispatchKeyItemManagementStatus(updateParams);
                }
            }
        }
    }

    /**
     * @param pkId
     * @return
     */
    @Override
    public List<DispatchKeyItemManagementSchedule> selectDispatchKeyItemManagementSchedulerListByPkId(String pkId) {
        List<DispatchKeyItemManagementSchedule> r = dispatchKeyItemManagementSchedulerMapper.selectDispatchKeyItemManagementSchedulerListByPkId(pkId);
        return r;
    }
}
